<?php 
if (!defined('BASEPATH')){
	exit('No direct script access allowed');
}

/**
 * ticketWorkers Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class TicketWorkers extends BACKEND_Controller {
	public function __construct() {
		parent::__construct();
		$this->load->language('ticketWorkers');
		$this->load->language('button');
		if($this->database_connect_status){
			$this->load->model('ticketWorkers_model');
			$this->set_controller('ticketWorkers');
			$this->set_model($this->ticketWorkers_model);
		}
                $this->load->model('ticketWorkers_model');

                $this->load->library('bookinglib');
                $this->bookinglib = new bookinglib();
	}
        
        protected function update($params=NULL){
		if($this->input->server('REQUEST_METHOD')=='POST'){
                        $this->view_data["ticketWorkers"]                                   = new stdClass();
                        $this->view_data["ticketWorkers"]->name                             = $this->input->post('name');
                        $this->view_data["ticketWorkers"]->status                           = $this->input->post('status');
                        $this->view_data["ticketWorkers"]->agent_code                       = $this->session->userdata['agent_code'];
                        $this->view_data["ticketWorkers"]->lastupdated                      = date("Y-m-d H:i:s",time());

                        $this->load->helper('form');
                        $this->load->helper('character');
                        $this->load->helper('form');
                        $this->load->library('form_validation');
                        $this->form_validation->set_error_delimiters('<div class="alert alert-error"><strong>'.$this->lang->line('error').': </strong>', '</div>');
                        $rules = array(
                            array(
                                'field'   => 'name',
                                'label'   =>  $this->lang->line('name'),
                                'rules'   => 'required|trim|max_length[50]|xss_clean'
                            )
                            ,array(
                                'field'   => 'status',
                                'label'   =>  $this->lang->line('status'),
                                'rules'   => 'required|numberic|max_length[1]|xss_clean'
                            )
                        );

                        $this->form_validation->set_error_delimiters('<div class="alert alert-error"><strong>'.$this->lang->line('error').': </strong>', '</div>');
                        $this->form_validation->set_rules($rules);

                        if ($this->form_validation->run()==TRUE){
                                if($params){
                                        //edit data
					$this->ticketWorkers_model->update($this->view_data["ticketWorkers"], $params);
                                        $logAction                              = '[UpdateTicketWorkers] '.$this->lang->line('update_ticketWorkers_success');
				}else{
					$params                                 = $this->ticketWorkers_model->create($this->view_data["ticketWorkers"]);
                                        $logAction                              = '[AddTicketWorkers] '.$this->lang->line('add_ticketWorkers_success');
				}
                                
				if($params){
					$this->session->set_flashdata('flash_message', $this->lang->line('update_successful'));
					$paramAdminLog  = array(
                                            'userid'            => $this->session->userdata['user_id'],
                                            'lastLogin'         => date('Y-m-d :H:i:s',time()),
                                            'ip'                => $_SERVER['REMOTE_ADDR'],
                                            'logAction'         => $logAction,
                                            'agent_code'        => $this->session->userdata['agent_code']
                                        );
                                        $this->user_model->insertUserAdminLog($paramAdminLog);
                                        redirect('auth/ticketWorkers');
				}
                        }
            }
            
            $this->load->model('ticketWorkers_model');
            $this->load->model('language_model');

            if(isset($params)){
                    $ticketWorkers_query	= $this->ticketWorkers_model->find_by(array('id'=>$params));
                    
                    if(!isset($ticketWorkers_query[0])){
                            $this->session->set_flashdata('flash_message', $this->lang->line('not_exists'));
                            redirect(site_url('auth/ticketWorkers'));
                            exit();
                    }

                    $this->view_data['ticketWorkers']				= $ticketWorkers_query[0];
            }

            $this->view_data['js'] = array(
                            base_url().'static/templates/backend/js/main.js'
            );
            $this->view_data['css'] = array(
            );
            $this->load->view('auth/ticketWorkers/edit', $this->view_data);
	}
        
}

/* End of file ticketWorkers.php */
/* Location: ./application/controllers/auth/ticketWorkers.php */